import numpy as np
from matplotlib import pyplot as plt
from matplotlib import rcParams
from matplotlib.axes import Axes
from scipy import stats


NUM = 2000
CHOOSE_NUM = 20

MEAN = 0
STD = 100

rcParams.update({
    'font.sans-serif': 'SimHei',
    'axes.unicode_minus': False,
})
# plt.tick_params(labelsize=29)
# plt.rcParams.update({
#     'font.sans-serif': 'SimHei',
#     'axes.unicode_minus': False,
# })
d = stats.norm(loc=MEAN, scale=STD)

x = np.arange(NUM)
y_hat = 15 * x + 3

epsilon = np.random.normal(MEAN, STD, NUM)
y = y_hat + epsilon

fig, (ax_func, ax_q) = plt.subplots(2, 1, figsize=(30, 30))
ax_func: Axes
ax_q: Axes

choose_x = x[:CHOOSE_NUM]
ax_func.plot(x[:CHOOSE_NUM], y_hat[:CHOOSE_NUM],
             color='red', label='y_hat = 5 * x + 3')
ax_func.scatter(choose_x, (15 * choose_x + 3 +
                epsilon[:CHOOSE_NUM]), color='blue')
# 设置x轴y轴提示内容
ax_func.set_xlabel('x轴', fontsize=30)
ax_func.set_ylabel('y轴', fontsize=30)
# 设置刻度字体大小
ax_func.tick_params(labelsize=30)
# 设置角落图例字体大小
ax_func.legend(fontsize=30)

delta = y - y_hat

up_actual, down_actual = np.quantile(delta, 0.2), np.quantile(delta, 0.8)
up_theoretical, down_theoretical = d.ppf(0.2), d.ppf(0.8)

ax_q.scatter(x, y - y_hat, color='blue')
ax_q.axhline(0, color='black')

ax_q.axhline(up_theoretical, label='0.8理论', color='cyan')
ax_q.axhline(down_theoretical, label='0.2理论', color='cyan')
ax_q.axhline(up_actual, label='0.8实际', color='magenta')
ax_q.axhline(down_actual, label='0.2实际', color='magenta')
ax_q.set_xlabel('x轴', fontsize=30)
ax_q.set_ylabel('y轴', fontsize=30)
ax_q.tick_params(labelsize=30)
ax_q.legend(fontsize=30)
plt.show()
